package com.itheima.controller;


import com.itheima.common.JsonResult;
import com.itheima.common.SnowFlake;
import com.itheima.pojo.admin.*;
import com.itheima.pojo.threadlocal.AuthThreadLocal;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * @author ChenJunJie
 * @date 2022/6/11 20:35
 * 员工controller层
 */
@RestController
@RequestMapping(value = "/employee", produces = "application/json;charset=utf-8")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SnowFlake snowFlake;
    @Resource
    private RedisTemplate<String, String> strRedisTemplate;


    //新增员工(注册)
    @PostMapping
    public JsonResult<String> employeeAdd(@RequestBody Employee employee, HttpServletRequest request) {

        long id = snowFlake.nextId();
        employee.setId(id + "");

        //获取用户名
        String username = AuthThreadLocal.getAuth();

        employee.setCreateUser(username);

        boolean b = employeeService.employeeAdd(employee);

        if (b) {
            return JsonResult.success("新增员工成功");
        } else {
            return JsonResult.error(employee.getUsername() + "已存在");
        }
    }

    //登录
    @PostMapping("/login")
    public JsonResult<Login> login(@RequestBody Employee employee) throws ParseException {

        return employeeService.login(employee);

    }

    //登出
    @PostMapping("/logout")
    public JsonResult<String> logout(HttpServletRequest request) {

        // employeeService.logout(request);

        return JsonResult.success("退出成功");

    }

    //员工分页查询接口
    @GetMapping("/page")
    public JsonResult<Pagination<EmployeePagination>> employeePagination(String name, @RequestParam("page") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {


        String newName = name;
        if (name != null && !"".equals(name)) {
            newName = ("%" + name + "%");
        }

        return employeeService.employeePaginationByName(newName, pageNum, pageSize);


    }

    //通过员工id查询员工
    @GetMapping("/{id}")
    public JsonResult<EmployeeById> employeeSelectById(@PathVariable String id) {
        EmployeeById employees = employeeService.employeeSelectById(id);

        if (employees != null) {
            return JsonResult.select(employees, "查询成功");
        } else {
            return JsonResult.error("查询失败");
        }
    }

    //通过员工id修改员工内容
    @PutMapping
    public JsonResult<EmployeeById> employeeUpdateById(@RequestBody EmployeeById employeeById,HttpServletRequest request) {


        //获取用户名
        String username = AuthThreadLocal.getAuth();

        employeeById.setUpdateUser(username);

        return employeeService.employeeUpdateById(employeeById);

    }

}
